package com.goldgov.learnmanage.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.gtiles.components.securityworkbench.bean.SwbAuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.learnmanage.service.Course;
import com.goldgov.learnmanage.service.CourseQuery;
import com.goldgov.learnmanage.service.CourseService;
import com.goldgov.learnmanage.service.User;
import com.goldgov.learnmanage.service.UserParam;
import com.goldgov.learnmanage.service.UserQuery;
import com.goldgov.learnmanage.service.UserService;
import com.goldgov.login.web.model.LoginAuthUser;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.PropertyUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/** 
 * @ClassName: LearnManageController
 * @Description: TODO
 * @author MaN
 * @date 2018年7月9日 
 */

@RestController
@RequestMapping("/workbench/learnmanage")
@Api("流动党员接口")
public class LearnManageController {

	@Autowired
	private CourseService courseService;
	
	@Autowired
	private UserService userService;
	
	@GetMapping("/listPartyMember")
	@ApiOperation(value = "流动党员列表", notes ="")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchTreePath", value = "查询路径", paramType = "query"),
			 @ApiImplicitParam(name = "searchUserName", value = "姓名", paramType = "query"),
			 @ApiImplicitParam(name = "searchMember", value = "是否是流动党员", paramType = "query")})
	public JsonQueryObject<User> listPartyMember(@ApiIgnore UserQuery query,HttpServletRequest request){
		SwbAuthUser user=LoginAuthUser.getUserByAdmin(request);
		if(null==query.getSearchTreePath() || "".equals(query.getSearchTreePath())) {
			query.setSearchOrgCode(user.getScopeCode());
		}
		List<User> list=userService.getUserList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@RequestMapping("/listLearnSituation")
	@ApiOperation(value = "流动党员学习情况", notes ="")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchUserID", value = "用户id", paramType = "query")})
	public JsonQueryObject<Course> listLearnSituation(@ApiIgnore CourseQuery query,HttpServletRequest request){
		List<Course> list=courseService.getCourseList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	@GetMapping("/getUserLearnList")
	@ApiOperation(value = "流动党员学习时长列表", notes ="")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchTreePath", value = "查询路径", paramType = "query"),
			 @ApiImplicitParam(name = "searchUserName", value = "姓名", paramType = "query"),
			 @ApiImplicitParam(name = "searchYear", value = "年度", paramType = "query"),
			 @ApiImplicitParam(name = "searchOrgName", value = "组织名称", paramType = "query"),
			 @ApiImplicitParam(name = "searchMember", value = "是否是流动党员", paramType = "query"),
			 @ApiImplicitParam(name = "searchChilderOrg", value = "是否包含子组织", paramType = "query")})
	public JsonQueryObject<User> getUserLearnList(@ApiIgnore UserQuery query,HttpServletRequest request){
		SwbAuthUser user=LoginAuthUser.getUserByAdmin(request);
		if(null==query.getSearchTreePath() || "".equals(query.getSearchTreePath())) {
			query.setSearchOrgCode(user.getScopeCode());
		}
		List<User> list=userService.getUserLearnList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	@GetMapping("/getCourseLearnTimeList")
	@ApiOperation(value = "流动党员课程学习总时长情况", notes ="")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchUserID", value = "用户id", paramType = "query"),
	@ApiImplicitParam(name = "searchYear", value = "年度", paramType = "query")})
	public JsonQueryObject<Course> getCourseLearnTimeList(@ApiIgnore CourseQuery query,HttpServletRequest request){
		List<Course> list=courseService.getCourseLearnTimeList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/findOrgLearn")
	@ApiOperation(value = "组织学习情况统计", notes ="组织学习情况统计")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchOrgID", value = "组织id", paramType = "query"),
		@ApiImplicitParam(name = "searchBeginDate", value = "开始时间", paramType = "query"),
		@ApiImplicitParam(name = "serarchNextNum", value = "子组织数量", paramType = "query"),
		@ApiImplicitParam(name = "searchType", value = "时间维度查询类型", paramType = "query"),
	@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query")})
	public JsonSuccessObject<List<User>> findOrgLearn(@ApiIgnore UserParam query,HttpServletRequest request){
		List<User> list = new ArrayList<>();
		if (PropertyUtil.objectNotEmpty(query.getSearchOrgID())) {
			//判断是否包含下级组织
//			List<User> judgeIsInclude = userService.judgeIsIncludeSubordinate(query.getSearchOrgID());
//			if (PropertyUtil.objectNotEmpty(query.getSerarchNextNum()) && query.getSerarchNextNum().intValue() > 0) {//包含下级组织是
				list = userService.findOrgLearnCourseTime(query);
//			}else {
//				list = userService.findOrgUserLearn(query);
//			}
		}
		
		JsonSuccessObject<List<User>> returnResult = new JsonSuccessObject<List<User>>(list);
		return returnResult;
	}
	
	@GetMapping("/findOrgUserContrast")
	@ApiOperation(value = "组织流动党员人员对比", notes ="组织流动党员人员对比")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchOrgID", value = "组织id", paramType = "query"),
		@ApiImplicitParam(name = "searchBeginDate", value = "开始时间", paramType = "query"),
		@ApiImplicitParam(name = "serarchNextNum", value = "子组织数量", paramType = "query"),
		@ApiImplicitParam(name = "searchOrderType", value = "查询类型", paramType = "query"),
		@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query")})
	public JsonQueryObject<User> findOrgUserContrast(@ApiIgnore UserQuery query,HttpServletRequest request){
		List<User> list = new ArrayList<>();
//		if (PropertyUtil.objectNotEmpty(query.getSerarchNextNum()) && query.getSerarchNextNum().intValue() > 0) {//包含下级组织是
			list = userService.findOrgUserContrast(query);
//		}else {
//			list = userService.findOrgUserContrastByOrgID(query);
//		}
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/findOrgLDDYStudy")
	@ApiOperation(value = "流动党员学习时长列表", notes ="")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchOrgID", value = "组织id", paramType = "query"),
		@ApiImplicitParam(name = "searchType", value = "时间维度查询类型", paramType = "query")})
	public JsonQueryObject<User> findOrgLDDYStudy(@ApiIgnore UserQuery query,HttpServletRequest request){
		List<User> list=userService.findOrgLDDYStudy(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/findCourseLearnHead")
	@ApiOperation(value = "课程学习热度", notes ="课程学习热度")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchOrgID", value = "组织id", paramType = "query"),
	@ApiImplicitParam(name = "searchYear", value = "年度", paramType = "query")})
	public JsonQueryObject<Course> findCourseLearnHead(@ApiIgnore CourseQuery query,HttpServletRequest request){
		List<Course> list=courseService.findCourseLearnHead(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	@GetMapping("/findOrgWCDContrast")
	@ApiOperation(value = "各组织课程学习时长对比", notes ="各组织课程学习时长对比")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchOrgID", value = "组织id", paramType = "query"),
		@ApiImplicitParam(name = "searchYear", value = "年度", paramType = "query"),
		@ApiImplicitParam(name = "serarchNextNum", value = "下级组织数量", paramType = "query"),
		@ApiImplicitParam(name = "searchOrganizationType", value = "机构类别", paramType = "query"),
		@ApiImplicitParam(name = "searchOrderType", value = "排序字段", paramType = "query")})
	public JsonQueryObject<Course> findOrgWCDContrast(@ApiIgnore CourseQuery query,HttpServletRequest request){
		List<Course> list= new ArrayList<>();
		if (PropertyUtil.objectNotEmpty(query.getSearchOrgID())) {//判断是否组织id是否为空
			int num = courseService.findOrgSGWXNum(query.getSearchOrgID());//查询是否窜在市工委县的组织
			if (num > 0) {//存在查询信息
				list=courseService.findOrgWCDContrast(query);
			}
		}else {//不存在直接查询
			list=courseService.findOrgWCDContrast(query);
		}
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/findLDDYStudyCourseListByUserId")
	@ApiOperation(value = "获取党员学习课程时长列表", notes ="获取党员学习课程时长列表")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchUserID", value = "党员id", paramType = "query"),
		@ApiImplicitParam(name = "searchType", value = "查询类型", paramType = "query")})
	public JsonQueryObject<Course> findLDDYStudyCourseListByUserId(@ApiIgnore CourseQuery query,HttpServletRequest request){
		List<Course> list= new ArrayList<>();
		if (PropertyUtil.objectNotEmpty(query.getSearchUserID())) {//判断是否组织id是否为空
			list = courseService.findLDDYStudyCourseListByUserId(query);
		}
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
}
